package com.burns.svg.demo;/**
 * Created by burns.
 *
 * @author <a href="http://www.esoon-soft.com/">burns</a>
 * @date 2021/04/25 13:52
 */

import org.apache.batik.anim.dom.SVGDOMImplementation;
import org.apache.batik.dom.GenericDOMImplementation;
import org.apache.batik.svggen.SVGGraphics2D;
import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import java.awt.*;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;

public class TestSVGGenerator1 {


    public static void main(String [] args) throws IOException {
        DOMImplementation impl = SVGDOMImplementation.getDOMImplementation();
        String svgNS = "http://www.w3.org/2000/svg";
        Document doc = impl.createDocument(svgNS, "svg", null);

// get the root element (the svg element)
        Element svgRoot = doc.getDocumentElement();

// set the width and height attribute on the svg root element
        svgRoot.setAttributeNS(null, "width", "400");
        svgRoot.setAttributeNS(null, "height", "450");

// create the rectangle
        Element rect = doc.createElementNS(svgNS, "rect");
        rect.setAttributeNS(null, "x", "10");
        rect.setAttributeNS(null, "y", "20");
        rect.setAttributeNS(null, "width", "100");
        rect.setAttributeNS(null, "height", "50");
        rect.setAttributeNS(null, "style", "fill:red");

// attach the rectangle to the svg root element
        svgRoot.appendChild(rect);

        SVGGraphics2D svgGenerator = new SVGGraphics2D(doc);
        // Finally, stream out SVG to the standard output using UTF-8
        // character to byte encoding
        boolean useCSS = true; // we want to use CSS style attribute
        Writer out = new OutputStreamWriter(System.out, "UTF-8");
        svgGenerator.stream(out, useCSS);
    }
}
